package com.example.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.blog.domain.ChangelogServer;
import com.example.blog.domain.Response;
import com.example.blog.note.LogInfo;
import com.example.blog.note.Permissions;
import com.example.blog.service.impl.ChangelogServerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@Transactional
@RequestMapping("/changelogServer")
public class ChangelogServerController {
    private final ChangelogServerServiceImpl changelogServerService;
    @Autowired
    public ChangelogServerController(ChangelogServerServiceImpl changelogServerService){
        this.changelogServerService = changelogServerService;
    }

    //获取所有更新日志
    @LogInfo("获取近七日流量记录")
    @GetMapping("/getSeven")
    public Response getAll(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR,-7);
        calendar.add(Calendar.DAY_OF_YEAR,-6);
        calendar.add(Calendar.DAY_OF_YEAR,-5);
        calendar.add(Calendar.DAY_OF_YEAR,-4);
        Date time = calendar.getTime();
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dayFormat = new SimpleDateFormat("dd");
        String date = dateFormat.format(time); //数据库查询需要的时间最小值
        String day = dayFormat.format(time); //最远的日期
        LambdaQueryWrapper<ChangelogServer> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(ChangelogServer::getDateTime).between(ChangelogServer::getDateTime,date,dateFormat.format(new Date()));
        List<ChangelogServer> list = changelogServerService.list(wrapper);
        //需要返回近七天内的   每天的登录人数  访问人数
        return new Response(Response.SUCCEED_CODE,"获取近七日流量记录",null);
    }

}
